import { query,add,remove,edit ,queryResources,queryLogs,queryOneUsergroup } from '@/services/meter_rate';
import moment from 'moment';
import {todayLastSecond} from '@/utils/utils.js'
import createRowData from '@/utils/createRowData';
import filter from 'lodash/filter'
import sortBy from 'lodash/sortBy'
export default {
  namespace: 'DMA_Statistics',
  state: {
    pageLoaded:false,
    tableY:300,
    data:[],
    summary:{},
    scrollData:{x:0,y:0},
    selectedRowKeys: [],
    searchParams:{
      interval_unit:'day',
      start_date:moment().add(-1,'month').format("YYYY-MM-DD"),
      end_date:moment().format("YYYY-MM-DD"),
    },
    searchParams2:{
      number:'',
      name:''
    },
    meta: {total: 0, per_page: 50,current_page:1},
    status_count:{total_count:0,normal_upload_count:0},
    total_consumption:{small_meter:{},big_meter:{}},
    additional: {total_count: 0, online_count: 0},
    loading: true,
    editRecord:{},
    pickRowData1:{
      number:'',
      name:'',
      data:[]
    },
    pickRowData2:{
      number:'',
      name:'',
      items:[]
    },
    detail:{
      pageLoaded:false,
      isDetail:false,
      detailId:'',
      data1:[],
      activeKey:'1',
      data2:[],
    }

  },
  effects: {
    *changeIsDetail({ payload, callback }, { call, put ,select}) {
      console.log('changeIsDetail')
      const {detail} = yield select(state => state.DMA_Statistics)
      yield put({
        type: 'changeDetail',
        payload:{
          ...detail,
          isDetail:!detail.isDetail,
          detailId:payload.id,
        }
      });
    },
    *reset({ payload, callback }, { call, put }) {
      console.log('reset=============')
      yield put({
        type: 'resetData',
        payload:payload
      });
      if(callback) callback()
    },
    *changeTableY({ payload, callback }, { call, put }) {
      const tableY=document.body.offsetHeight -
        document.querySelector('.table-data-content').getBoundingClientRect().top - 20;
      yield put({
        type: 'saveTableY',
        payload: tableY-42
      });
    },
    *changeSearchParams({ payload, callback }, { call, put }) {
      console.log('changeSearchParams')
      yield put({
        type: 'saveSearchParams',
        payload: payload
      });
    },
    *changeLoaded({ payload, callback }, { call, put }) {
      yield put({
        type: 'changePageLoaded',
        payload: true,
      });
    },
    *changeScroll({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeScrollData',
        payload: payload,
      });
    },
    *selectRow({payload, callback}, {call, put,select}) {
      const {selectedRowKeys} = yield select(state => state.DMA_Statistics)
      if (selectedRowKeys.indexOf(payload.id) >= 0) {
        selectedRowKeys.splice(selectedRowKeys.indexOf(payload.id), 1);
      } else {
        selectedRowKeys.push(payload.id);
      }
      yield put({
        type: 'saveSelectRow',
        payload: selectedRowKeys,
      });
    },
    *fetch({ payload,callback }, { call, put ,select}) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const state = yield select(state => state.DMA_Statistics)//获取state中的数据
      payload=payload||state.searchParams;
      console.log('payload',payload)
      const response = yield call(query, payload);
      console.log(response)
      if(response.status===200){
        yield put({
          type: 'saveDMA',
          payload:  response.data
        });
        yield put({
          type: 'saveSearchParams',
          payload: payload
        });
        yield put({
          type: 'changeLoading',
          payload: false,
        });
        if (callback) callback();
      }
    },
    *fetchRowData({ payload,callback }, { call, put ,select}) {

    },
    *changeSelectRow({payload, callback}, {call, put,select}) {
      yield put({
        type: 'saveSelectRow',
        payload: payload.selectedIndex,
      });
      console.log('选着的行数据 payload',payload)
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      yield put({
        type: 'saveNode',
        payload: {
          key:payload.row.key,
          name:payload.row.name,
          data:payload.row.graph_data,
          id:payload.row.id,
        }
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *changeSelectRow2({payload, callback}, {call, put,select}) {
      yield put({
        type: 'saveSelectRow',
        payload: payload.selectedIndex,
      });
      console.log('选着水表数据 payload',payload)
      yield put({
        type: 'saveItem',
        payload: {
          name:payload.row.name,
          items:payload.row.items,
        }
      });
    },
    *resetSelect({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveResetSelect',
      });
    },
    *resetSelect2({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveResetSelect2',
      });
    },
  },

  reducers: {
    saveResetSelect(state, action) {
      return {
        ...state,
        pickRowData1:{
          number:'',
          name:'',
          data:[]
        },
        pickRowData2:{
          number:'',
          name:'',
          items:[]
        },
      };
    },
    saveResetSelect2(state, action) {
      return {
        ...state,
        pickRowData2:{
          number:'',
          name:'',
          items:[]
        },
      };
    },
    saveDMA(state, action) {
      console.log('action.payload',action.payload)
      return {
        ...state,
        data: action.payload.data,
      };
    },
    saveNode(state, action) {
      console.log('saveNode action.payload',action.payload)
      let parserData=sortBy(action.payload.data.vertexs,function(o){
        return -o.type
      })
      console.log('parserData',parserData)
      return {
        ...state,
        pickRowData1: {
          key:action.payload.key,
          name:action.payload.name,
          data:parserData,
          id:action.payload.id,
        },
      };
    },
    saveItem(state, action) {
      console.log('saveItem action.payload',action.payload)
      return {
        ...state,
        pickRowData2: {
          name:action.payload.name,
          items:action.payload.items,
        },
      };
    },
    save(state, action) {
      console.log('action.payload',action.payload)
      let parserData=sortBy(filter(action.payload.data.vertexs,function(o){
        return o.children.length>0
      }),function(o){
        return -o.type
      })
      return {
        ...state,
        data: {
          key:action.payload.key,
          name:action.payload.name,
          data:parserData,
          id:action.payload.id,
        },
      };
    },
    saveRowData1(state, action) {
      return {
        ...state,
        pickRowData:action.payload,
        // summary:action.payload.data.summary,
      };
    },
    saveRowData(state, action) {
      return {
        ...state,
        pickRowData:action.payload,
        // summary:action.payload.data.summary,
      };
    },
    saveTableY(state, action) {
      return {
        ...state,
        tableY: action.payload,
      };
    },
    saveOneRecord(state, action) {
      return {
        ...state,
        editRecord:action.payload
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    changePageLoaded(state, action) {
      return {
        ...state,
        pageLoaded: action.payload,
      };
    },
    changeScrollData(state, action) {
      return {
        ...state,
        scrollData: action.payload,
      };
    },
    saveSearchParams(state, action) {
      return {
        ...state,
        searchParams: {
          ...state.searchParams,
          ...action.payload
        },
      };
    },
    saveSearchParams2(state, action) {
      return {
        ...state,
        searchParams2: {
          ...state.searchParams,
          ...action.payload
        },
      };
    },
    resetData(state, action) {
      return  {
        pageLoaded:action.payload,
        tableY:state.tableY,
        data:[],
        summary:{},
        scrollData:{x:0,y:0},
        selectedRowKeys: [],
        searchParams:{
          interval_unit:'day',
          start_date:moment().add(-1,'month').format("YYYY-MM-DD"),
          end_date:moment().format("YYYY-MM-DD"),
        },
        searchParams2:{
          number:'',
          name:''
        },
        meta: {total: 0, per_page: 50,current_page:1},
        status_count:{total_count:0,normal_upload_count:0},
        date_range_consumption:{small_meter:{},big_meter:{}},
        total_consumption:{small_meter:{},big_meter:{}},
        loading: true,
        editRecord:{},
        pickRowData:{
          number:'',
          name:'',
          data:[]
        },
        additional: {total_count: 0, online_count: 0},
        detail:{
          pageLoaded:false,
          isDetail:false,
          detailId:'',
          data1:[],
          activeKey:'1',
          data2:[],
        }
      };
    },
    saveResources(state, action) {
      return {
        ...state,
        detail:{
          ...state.detail,
          data1: action.payload.data,
        }
      };
    },
    saveLogs(state, action) {
      return {
        ...state,
        detail:{
          ...state.detail,
          data2: action.payload.data,
          meta2:action.payload.meta
        }
      };
    },
    saveSelectRow(state, action) {
      return {
        ...state,
        selectedRowKeys: action.payload,
      };
    },
    changeDetail(state, action) {
      return {
        ...state,
        detail:action.payload
      };
    },
    changeDetailTabActive(state, action) {
      return {
        ...state,
        detail:{
          ...state.detail,
          activeKey:action.payload
        }
      };
    },
    saveDetailSearchParams(state, action) {
      return {
        ...state,
        detail:{
          ...state.detail,
          searchParams2: action.payload,
        }

      };
    },
  },

};
